package com.flying.carclub.http;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

/*
 * Created by xcheng on 2015/9/16.
 */
public abstract class ApiHandler extends Handler {
    private String TAG="https";
    private Context context;

    //获取网络数据并解析成功
    public abstract void onSuccess(String result);


    //获取网络数据失败
    public abstract void onFail(OkHttpStatus status);


    public ApiHandler(Context context) {
        this.context = context;
    }
    public ApiHandler() {

    }

    public ApiHandler(Context context, Looper looper) {
        super(looper);
        this.context = context;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.obj instanceof ApiHandleParams) {
            ApiHandleParams apiHandleParams = (ApiHandleParams) msg.obj;
            switch (apiHandleParams.getOkHttpStatus()) {
                case GET_CACHE_SUCCESS:
                case SUCCESS:
                    try {
                       /* if (context != null && !TextUtils.isEmpty(apiHandleParams.getToken())) {
                            ArrayList<DxlBaseLoadingLayout> loadingLayouts = apiHandleParams.getLoadingLayouts();
                            for (DxlBaseLoadingLayout loadingLayout : loadingLayouts) {
                                if (loadingLayout != null) {
                                    loadingLayout.loadSuccess();
                                }
                            }
                        }*/
                        if (apiHandleParams.getParser()!=null) {
                            Log.d(TAG, "暂时不支持解析");
                        }else {
                            onSuccess(apiHandleParams.getResponse().toString());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        /*if (context != null && !TextUtils.isEmpty(apiHandleParams.getToken())) {
                            ArrayList<DxlBaseLoadingLayout> loadingLayouts = apiHandleParams.getLoadingLayouts();
                            for (DxlBaseLoadingLayout loadingLayout : loadingLayouts) {
                                if (loadingLayout != null) {
                                    loadingLayout.loadSuccess();
                                }
                            }
                        }*/
                        onSuccess(apiHandleParams.getResponse().toString());
                    }
                    break;
                case NO_NETWORK:
//                    ErrorHandler.showMessage(R.string.error_no_response);
//                    apiHandleParams.getOkHttpStatus().setMessage(ZHBApplication.getInstance().getString(R.string.error_no_response));
                    onFail(apiHandleParams.getOkHttpStatus());
                    break;
                default:
                   /* ArrayList<DxlBaseLoadingLayout> loadingLayouts = apiHandleParams.getLoadingLayouts();
                    for (DxlBaseLoadingLayout loadingLayout : loadingLayouts) {
                        if (loadingLayout != null) {
                            loadingLayout.loadFail();
                        }
                    }*/
                    if (apiHandleParams.isShowError()) {
//                        ErrorHandler.showMessage(apiHandleParams.getOkHttpStatus().getMessage());
                    }
                    onFail(apiHandleParams.getOkHttpStatus());
            }
        }

    }
}
